import { IMAGE_TYPE, isUseQiniu } from './constance'
import store from '@/store/index.js'
import {getCurrentDate} from '@/utils/m7Utils'
import * as uuid from 'uuid'
import {
  qiniu7moorDomain,
  qiniu7moorKmDomain
} from './qiniuUtils'
import { transUrl } from './publicMethod'
import { checkImgBySuffix } from './qiniuPluload'

export const km = {
  domain (url = '') {
    return url.startsWith('cos/') ? store.state.fileDomain.cos7moorKmDomain : qiniu7moorKmDomain
  },
  getKmUrl (oFile, options = {}) { // 获取km的链接
    const { useAttname = true, isOriginFile = false } = options
    let file = oFile
    if (!isOriginFile) {
      file = oFile.id ? oFile : Object.entries(oFile).map(([id, name]) => ({id, name}))[0]
    }
    let url = file.id || ''
    const isCosUrl = url.startsWith('cos/')
    if (isCosUrl) {
      url = file.id.includes('.') ? file.id : `${file.id}${file.name}`
    }
    const attname = useAttname ? `?attname=${file.name}` : ''
    let res = `${this.domain(url)}${url}${attname}`
    if (isCosUrl) {
      res = `${res}${res.includes('?') ? '&response-content-disposition=attachment' : '?response-content-disposition=attachment'}`
    }
    return res
  },
  getAttach (map, item) {
    if (isUseQiniu()) {
      map[item.id] = item.name
    } else {
      const arr = item.id.split(item.name)
      map[arr[0]] = item.name
    }
  },
  getFileKey ({up, info, isLink, file} = {}) {
    const cmpFile = file || up
    const fileKey = cmpFile.fileKey || cmpFile.key
    const domain = this.domain(fileKey)
    return isLink ? `${domain}${fileKey}` : fileKey
  },
  getUploadKey (_, file) {
    const account = store.state.session.user.account
    const date = getCurrentDate()
    const today = new Date()
    const time = today.getTime()
    const key = (isUseQiniu() ? '' : 'cos/') + account + '/km/' + date + '/' + time + '/' + uuid.v1() + '/' + (isUseQiniu() ? '' : file.name)
    return key
  },
  getCosKey (key) { // /cos/作为新老数据的区分
    return isUseQiniu() ? key : `cos/${key}`
  }
}

export const common = { // 除了km外的其他模块cos上传方法
  getLinkUrl (oFile, options = { }) { // 获取链接
    const { useAttname = true, up, useDomain = true } = options
    const domain = up.getOption('domain')
    const file = oFile.id ? oFile : Object.entries(oFile).map(([id, name]) => ({id, name}))[0]
    const url = file.id.includes('.') ? file.id : `${file.id}${file.name}`
    const attname = useAttname ? `?attname=${file.name}` : ''
    let res = `${useDomain ? domain : false}${url}${attname}`
    res = `${res}${res.includes('?') ? '&response-content-disposition=attachment' : '?response-content-disposition=attachment'}`
    return res
  },
  getFileKey ({up, info, isLink, file} = {}) {
    const domain = up.getOption('domain')
    const res = !info ? up : JSON.parse(info || '{}')
    const cmpFile = file || up
    const fileKey = isUseQiniu() ? res.key : cmpFile.fileKey
    return isLink ? `${domain}${fileKey}` : fileKey
  },
  getUrl (file, options) {
    const { domain = qiniu7moorDomain, useAttname = false, noQuery = false, domainKey = '', preDomain = '', idKey = 'id' } = options || {}
    const isQiniuDomain = domain === qiniu7moorDomain
    const cmpDomain = store.state.fileDomain[domainKey] ? store.state.fileDomain[domainKey] : domain
    const id = file[idKey]
    const cmpId = (String(id) || '').startsWith('http') ? id : file.name
    const isFullUrl = (String(id) || '').startsWith('http') || (file.name || '').startsWith('http')
    // const cmpImgQuery = IMAGE_TYPE.some(i => (file.type || '').includes(i)) ? 'imageView2/1/w/50' : ''
    const cmpImgQuery = ''
    const url = `${isQiniuDomain ? preDomain : ''}${transUrl(isFullUrl ? cmpId : `${cmpDomain}${id}`)}`
    const isCosUrl = (url || '').startsWith('https://cc')
    const cmpUseAttname = (isQiniuDomain && !isCosUrl) ? true : useAttname
    const query = noQuery ? '' : `?${cmpUseAttname ? `attname=${file.name}` : cmpImgQuery}`
    let res = `${isQiniuDomain ? preDomain : ''}${transUrl(isFullUrl ? `${cmpId}${query}` : `${cmpDomain}${id}${query}`)}`
    if (!noQuery) {
      res = `${res}${res.includes('?') ? '&response-content-disposition=attachment' : '?response-content-disposition=attachment'}`
    }
    return res
  },
  addAttname (attach) {
    return `${attach.url}?attname=${attach.name}`
  },
  addCosDown (url, ignoreImg) {
    const isImg = checkImgBySuffix(url)
    if (isImg && ignoreImg) {
      return url
    }
    const res = `${url}${url.includes('?') ? '&response-content-disposition=attachment' : '?response-content-disposition=attachment'}`
    return res
  },
  urlDeal (url) {
    let newUrl = ''
    if(['heic','HEIC'].includes(url.substring(url.lastIndexOf('.') + 1))) {
      if(url.indexOf('?') > 0) {
        url = url.substring(url.indexOf('?'), 0) + '?imageView2/2/w/500/h/300'
      } else {
        url = url + '?imageView2/2/w/200/h/200'
      }
      newUrl = url
    } else {
      newUrl = url.indexOf('?') > 0 ? url.substring(url.indexOf('?'), 0) : url
    }
    return newUrl
  },
  quickUrlDeal (url) {
    let newUrl = ''
    if(['heic','HEIC'].includes(url.substring(url.lastIndexOf('.') + 1))) {
      if(url.indexOf('?') > 0) {
        url = url.substring(url.indexOf('?'), 0) + '?imageView2/2/w/500/h/10000'
      } else {
        url = url + '?imageView2/2/w/10000'
      }
      newUrl = url
    } else {
      newUrl = url.indexOf('?') > 0 ? url.substring(url.indexOf('?'), 0) : url
    }
    return newUrl
  }
}
